package com.ytjj.qmyx.supplychain.api.controller;

import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.ReconciliationOtherMoneyRequest;
import com.ytjj.qmyx.supplychain.common.model.request.ReconciliationRequest;
import com.ytjj.qmyx.supplychain.service.ReconciliationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 财务管理
 * <p>
 * 财务对账
 */

@Slf4j
@RestController
@RequestMapping("/reconciliation")
public class ReconciliationController {

    @Resource
    private ReconciliationService reconciliationService;

    /**
     * 根据条件获取账单
     */
    @PostMapping("/getReconciliationList")
    public CommonResult getReconciliationList(@RequestBody ReconciliationRequest request) {
        return reconciliationService.getReconciliationList(request);
    }

    /**
     * 根据账单id查该账单的订单信息
     */
    @GetMapping("/getReconciliationOrders")
    public CommonResult getReconciliationOrders(@RequestParam Integer id,
                                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return reconciliationService.getReconciliationOrders(id, pageNum, pageSize);
    }

    /**
     * 根据账单id查该账单的订单信息
     */
    @GetMapping("/getReconciliationReturn")
    public CommonResult getReconciliationReturn(@RequestParam Integer id,
                                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return reconciliationService.getReconciliationReturn(id, pageNum, pageSize);
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    public CommonResult audit(@RequestParam Integer id, @RequestParam String auditType, @RequestParam(required = false) String failRemark) {
        return reconciliationService.audit(id, auditType, failRemark);
    }

    /**
     * 上传凭证
     */
    @GetMapping("/uploadVoucher")
    public CommonResult uploadVoucher(@RequestParam Integer id, @RequestParam String voucherImg) {
        return reconciliationService.uploadVoucher(id, voucherImg);
    }

    /**
     * 账单上传其他金额表格文件
     * @param request
     */
    @PostMapping("/uploadFile")
    public CommonResult uploadFile(@RequestBody List<ReconciliationOtherMoneyRequest> request) throws IOException {
        Map map = reconciliationService.uploadDate(request, request.get(0).getReconciliationId(), request.get(0).getFileName(), "supplyChain");
        return CommonResult.success(map);
    }

    /**
     * 获取账单上传的文件
     */
    @GetMapping("/getFileName")
    public CommonResult getFileName(@RequestParam Integer reconciliationId) {
        return reconciliationService.getFileName(reconciliationId);
    }

    /**
     * 获取上传文件的数据
     * @param reconciliationId
     * @param source
     * @return
     */
    @GetMapping("/getOtherMoneyDate")
    public CommonResult getOtherMoneyDate(@RequestParam Integer reconciliationId, @RequestParam String source) {
        return reconciliationService.getOtherMoneyDate(reconciliationId,source);
    }


    /**
     * 下载账单
     */
    @GetMapping("/downloadReconciliation")
    public CommonResult downloadReconciliation(@RequestParam Integer reconciliationId){
        return reconciliationService.downloadReconciliation(reconciliationId);
    }
}
